﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using SurvivalStrategy.Modules.ActionArgs;
using SurvivalStrategy.Modules.Memory;
using SurvivalStrategy.Entities.EnitityComponents;

namespace SurvivalStrategy.Modules.ActionModules
{
    class StandardMovingModule : ActionModule
    {
        public StandardMovingModule() : base()
        {
            AddFunction(new ActionModuleFunc(Move), new ActionModulePriority(MovePriority), ActionTags.Moving); 
            // projektant modulu sam musi uzupelnic ta czesc
            // dopisuje tu swoje metody do listy z odpowiednimi tagami
        }

        private bool Move(ActionModuleArgs args)
        {
            StandardMovingArgs movingArgs = (StandardMovingArgs)args;
            if (movingArgs.Point != Vector3.Zero)
            {
                Vector3 pos = GetPosition();

                Vector3 newPos = new Vector3(pos.X + movingArgs.Point.X, pos.Y + movingArgs.Point.Y, pos.Z + movingArgs.Point.Z);

                SetPosition(newPos);

                int countCollisions = (from p in GetWorldElementAbsolutePosition(newPos) where p.Collidable == true && p.CheckCollision(this.GetComponent<EntitiyCollider>().Collider) == true select p).Count<WorldData.WorldElement>();
    
                //w razie rozbudowanych siatek uzyc foreacha i breaka

                if (countCollisions > 0)   //jezeli niczego blokujacego nie ma przed nim
                {
                    SetPosition(pos);
                }
                GetComponent<SurvivalComponent>().UseEnergy(0.03f);
            }
            return true;
        }

        private int MovePriority()
        {
            return 0;
        }
    }
}
